package com.example.demo.service.impl;

import com.example.demo.mapper.StudentCertificationMapper;
import com.example.demo.mapper.StudentLoginMapper;
import com.example.demo.mapper.entity.Student;
import com.example.demo.mapper.entity.StudentCertification;
import com.example.demo.service.StudentCertificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Lucy
 * @create 2024-01-30 22:07
 */
@Service
public class StudentCertificationServiceImpl implements StudentCertificationService {
    @Autowired
    private StudentCertificationMapper mapper;

    @Override
    public Student selectStudentById(int id) {
        return mapper.selectStudentById(id);
    }

    @Override
    public void placeIdCardFace(StudentCertification cardFace) {
        int exist = mapper.selectByNumber(cardFace.getStudentNumber());
        if(exist==0){
            mapper.insertCertification(cardFace);
        }
        mapper.placeIdCardFace(cardFace);
    }

    @Override
    public void placeIdCardNational(StudentCertification national) {
        int exist = mapper.selectByNumber(national.getStudentNumber());
        if(exist==0){
            mapper.insertCertification(national);
        }
        mapper.placeIdCardNational(national);
    }

    @Override
    public void placeAdmissionNotice(StudentCertification admission) {
        int exist = mapper.selectByNumber(admission.getStudentNumber());
        if(exist==0){
            mapper.insertCertification(admission);
        }
        mapper.placeAdmissionNotice(admission);
    }

    @Override
    public int selectPhoneCount(String phone) {
        return mapper.selectPhoneCount(phone);
    }

    @Override
    public int selectEmailCount(String email) {
        return mapper.selectEmailCount(email);
    }

    @Override
    public int updateStudentState(Student currentStudent) {
        return mapper.updateStudentState(currentStudent);
    }

    @Override
    public void placeBareheadedPhoto(StudentCertification bareheaded) {
        Student currentStudent=new Student();
        currentStudent.setNumber(bareheaded.getStudentNumber());
        currentStudent.setBareheadedPhoto(bareheaded.getBareheadedPhoto());

        int exist = mapper.selectByNumber(bareheaded.getStudentNumber());
        if(exist==0){
            bareheaded.setState(2);
            mapper.insertCertification(bareheaded);
            mapper.updateBareheadedPhoto(currentStudent);
        }else{
            mapper.placeBareheadedPhoto(bareheaded);
            mapper.updateBareheadedPhoto(currentStudent);
        }
    }

    @Override
    public int exitProcess(Student currentStudent) {
        int exist = mapper.selectByNumber(currentStudent.getNumber());
        if(exist == 0) {
            int result = mapper.updateStudentNull(currentStudent);
            if (result == 0) return 0;
        }else{
            int result1 = mapper.deleteCertification(currentStudent);
            int result2 = mapper.updateStudentNull(currentStudent);
            if (result1 == 0) return 0;
            if (result2 == 0) return 0;
        }
        return 1;
    }

    @Override
    public int updateStudentPhone(Student currentStudent) {
        return mapper.updateStudentPhone(currentStudent);
    }

    @Override
    public int updateStudentEmail(Student currentStudent) {
        return mapper.updateStudentEmail(currentStudent);
    }


}
